package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.config.GlobConfig;
import com.jbp.dao.LuckRankDao;
import com.jbp.model.*;
import com.jbp.service.LuckRankService;
import com.jbp.service.UserLevelService;
import com.jbp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * 手气榜(LuckRank)表服务实现类
 * @author cc
 * @since 2021-07-20 19:57:48
 */
@Service
public class LuckRankServiceImpl extends ServiceImpl<LuckRankDao, LuckRank> implements LuckRankService {
    @Autowired
    private UserService userService;
    @Autowired
    private UserLevelService userLevelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
//        LocalDate today = LocalDate.now();
        // 获取当前时间
//        LocalDateTime now = LocalDateTime.now();
        // 获取两天前的时间（包含今天和昨天）
//        LocalDateTime twoDaysAgo = now.minusDays(1).with(LocalTime.MIN);
        QueryWrapper<LuckRank> queryWrapper = new QueryWrapper<>();
        /*if (params.get("poolType") != null){
            String poolType = (String) params.get("poolType");
            queryWrapper.in("pool_type", poolType.split(","));
        }
        queryWrapper.orderByDesc("create_time");*/
        /*queryWrapper.select("user_id", "MAX(gift_price) as gift_price")
                .groupBy("user_id").orderByDesc("gift_price");*/
        queryWrapper.select("user_id", "gift_price").orderByDesc("create_time");
        if (params.get("poolType") != null){
            String poolType = (String) params.get("poolType");
            queryWrapper.in("pool_type", poolType.split(","));
        }
//        queryWrapper.apply("DATE(create_time) = {0}", today);//当天
//        queryWrapper.between("create_time", twoDaysAgo, now);
        IPage<LuckRank> page = this.page(
                new Query<LuckRank>().getPage(params),
                queryWrapper
        );

        for (LuckRank record : page.getRecords()) {
            User user = userService.getById(record.getUserId());
            record.setGiftNum(record.getGiftPrice());
            if (null != user) {
                record.setUserLevel(user.getLevel());
                record.setUserLevelOn(userLevelService.levelOn(user.getId()));
                record.setUserImg(user.getImg());
                record.setUserName(user.getName());
                record.setSex(user.getSex());
            }
        }

        return new PageUtils(page);
    }

    @Override
    public void saveLuckRank(int price, LotteryRecord lotteryRecord, List<Gift> giftList) {
        LuckRank luckRank = new LuckRank();
        luckRank.setUserId(lotteryRecord.getUserId());
        luckRank.setLotteryRecordId(lotteryRecord.getId());
        luckRank.setPoolType(lotteryRecord.getPoolType());
        luckRank.setRoomId(lotteryRecord.getRoomId());
        luckRank.setTimes(GlobConfig.getType(lotteryRecord.getType()));

        giftList.stream().filter(gift -> gift.getCoin()>1000 || price>=1000)
                .sorted(Comparator.comparingInt(Gift::getCoin))
                .forEach(gift -> {
                    luckRank.setGiftImg(gift.getImgUrl());
                    luckRank.setGiftPrice(gift.getCoin());
                    String name = gift.getName() + "(" + gift.getCoin() + ")x" + gift.getNum() + " ";
                    if (luckRank.getGiftName()!=null) {
                        luckRank.setGiftName(luckRank.getGiftName() + name);
                    } else {
                        luckRank.setGiftName(name);
                    }
                });

        if (luckRank.getGiftPrice()!=null) {
            save(luckRank);
        }
    }
}